package com.wzk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzk.mapper.EmpExprMapper;
import com.wzk.mapper.EmpMapper;
import com.wzk.pojo.*;
import com.wzk.service.EmpService;
import com.wzk.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
    }
    @Override
    public void deleteByEmpIds(List<Integer> empids) {
        empExprMapper.deleteByEmpIds(empids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getInfo(id);
    }
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if(!exprList.isEmpty()){
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public List<Emp> list() {
        return empMapper.list();
    }

    @Override
    public LoginInfo login(Emp emp) {

        Emp e = empMapper.selectByUsernameAndPassword(emp);
        if(e!=null){
            log.info("登录成功: {}", e);
            Map<String, Object> claims=new HashMap<>();
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            String token= JwtUtils.generateJwt(claims);
            return new LoginInfo(e.getId(), e.getUsername(), e.getName(), token);
        }
        return null;

    }


    @Override
    public PageResult<Emp> pageSelect(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());//这里调用pagehelper的方法，就调用startpage，参数传页码和页数据量
        List<Emp> emps = empMapper.pageSelect(empQueryParam);
        Page<Emp> empPage = (Page<Emp>) emps;//pagehelper用完之后底层会有这个类，这个类底层就是继承的list，所以可以直接强转
        //Page里面还有各种变量比如total之类的，所以返回结果类的时候直接从这里get出来就可以了
        return new PageResult<Emp>(empPage.getTotal(), empPage.getResult());//这里就是调出的变量
    }
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        List<EmpExpr> exprList = emp.getExprList();
        if(!exprList.isEmpty()){
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }
    }
}
